public class test02 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        HashMap<Integer,Integer> hashmap;
        public TreeNode buildTree(int[] inorder, int[] postorder) {
            hashmap = new HashMap<>();
            for(int i = 0;i<inorder.length;i++){
                hashmap.put(inorder[i],i);
            }
            //左闭右开
            return buildRoot(inorder,0,inorder.length,postorder,0,postorder.length);
        }
        private TreeNode buildRoot(int[] inorder,int inBegin,int inEnd,int[]postorder,int postBegin,int postEnd){
            if(inBegin>=inEnd || postBegin>=postEnd){
                return null;
            }
            int index = hashmap.get(postorder[postEnd-1]);
            TreeNode root = new TreeNode(inorder[index]);
            int lenOfLeft = index-inBegin;
            root.left = buildRoot(inorder,inBegin,index,postorder,postBegin,postBegin+lenOfLeft);
            root.right = buildRoot(inorder,index+1,inEnd,postorder,postBegin+lenOfLeft,postEnd-1);
            return root;
        }
}
